<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Appoint;
use App\Models\Balance;
use App\Models\HoneyBalance;
use App\Models\Material;
use App\Models\Order;
use App\Models\User;
use Dingo\Api\Http\Request;

class BalanceController extends BaseController
{
    public $model = Balance::class;



    public function flow(Request $request) {
        $user_id = $this->getUserId();
        $list = Balance::where('user_id', $user_id)->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));
        foreach ($list as &$item) {
            $item->entity = null;
            if (in_array($item->type, [Balance::TYPE_MATERIAL, Balance::TYPE_APPOINT])) {
                $order = Order::find($item->order_id);
                if ($order->entity_type == Order::ENTITY_MATERIAL) {
                    $item->material = Material::where('id', $order->entity_id)->first(['title', 'id']);
                } else {
                    $item->appoint = Appoint::withCertain('service', ['title', 'category'])->select(['id', 'category', 'service_id'])->find($order->entity_id);
                }
            }

        }
        return $this->paginate($list);
    }

    public function detail() {
        $user = $this->getUser();
        $data['balance'] = $user->balance_yuan;
        return $this->success($data);
    }
}

